<!DOCTYPE HTML>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/pointer-lock/pointer-lock-test-harness.js"></script>
</head>
<body id="body">
<div>
  <div id="target1"></div>
  <div id="target2"></div>
</div>
<script>
    description("Test mouse events are routed to lock target.")
    window.jsTestIsAsync = true;

    targetdiv1 = document.getElementById("target1");
    targetdiv2 = document.getElementById("target2");

    // Expect change event only for all transitions below.
    expectedTargetToBeLockedString = "";
    document.onpointerlockchange = function ()
        {
            testPassed("document.onpointerlockchange event received.");
            shouldBe("document.pointerLockElement", expectedTargetToBeLockedString);
            doNextStep();
        };
    document.onpointerlockerror =
        function () { testFailed("document.onpointerlockerror event received."); finishJSTest(); };

    function eventNotExpected(e) {
        testFailed("Unexpected event " + e.type + " on " + e.currentTarget.id);
        finishJSTest();
    }
    function eventExpected(e) {
      testPassed("event type: " + e.type + ", target: " + e.target.id + ", received on: " + e.currentTarget.id);
    }

    todo = [
        function () {
            targetdiv1.requestPointerLock();
            expectedTargetToBeLockedString = "targetdiv1";
            // doNextStep() called by onpointerlockchange handler.
        },
        function () {
            debug("     With a lock in place send a click.")
            targetdiv1.onmousedown = eventExpected;
            targetdiv2.onmousedown = eventNotExpected;
            document.body.onmousedown = eventExpected;
            if (window.eventSender) {
                eventSender.leapForward(1000);
                eventSender.mouseDown();
                eventSender.mouseUp();
            }
            doNextStepWithUserGesture();
        },
        function () {
            targetdiv2.requestPointerLock();
            expectedTargetToBeLockedString = "targetdiv2";
            // doNextStep() called by onpointerlockchange handler.
        },
        function () {
            debug("     With a lock in place send a click.")
            targetdiv1.onmousedown = eventNotExpected;
            targetdiv2.onmousedown = eventExpected;
            document.body.onmousedown = eventExpected;
            if (window.eventSender) {
                eventSender.leapForward(1000);
                eventSender.mouseDown();
                eventSender.mouseUp();
            }
            doNextStepWithUserGesture();
        },
        function () {
            debug("     Ensure double clicks work as expected, sending 4 clicks.")
            targetdiv1.onmousedown = undefined;
            document.body.onmousedown = undefined;
            targetdiv2.onmousedown = eventExpected;
            targetdiv2.onmouseup = eventExpected;
            targetdiv2.onclick = eventExpected;
            targetdiv2.ondblclick = eventExpected;
            debug("     Creating a click and dblclick.");
            if (window.eventSender) {
                eventSender.leapForward(1000);
                eventSender.mouseDown();
                eventSender.mouseUp();
                eventSender.mouseDown();
                eventSender.mouseUp();
            }
            debug("     Ensuring no false dblclicks if we continue.");
            if (window.eventSender) {
                eventSender.mouseDown();
                eventSender.mouseUp();
                eventSender.mouseDown();
                eventSender.mouseUp();
            }
            doNextStepWithUserGesture();
        },
    ];
    doNextStepWithUserGesture();
</script>
</body>
</html>
